package edu.hubu.talentmanagementsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.CaseFormat;
import edu.hubu.talentmanagementsystem.mapper.BlogMapper;
import edu.hubu.talentmanagementsystem.mapper.context.MapperContext;
import edu.hubu.talentmanagementsystem.model.ext.Blog;
import edu.hubu.talentmanagementsystem.service.IBlogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import edu.hubu.talentmanagementsystem.common.EncodeOperation;
import edu.hubu.talentmanagementsystem.common.EncodeParam;

import java.util.List;
import java.util.Optional;

/**
 * @author moonlan
 * date 2021/3/2 下午8:45
 */
@SuppressWarnings(value = "unused")
@Service
@Transactional(rollbackFor = Exception.class)
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements IBlogService {

    private final MapperContext context;

    public BlogServiceImpl(MapperContext context) {this.context = context;}

    @Override
    public boolean deleteById(Integer id) {
        return context.getBlogMapper().deleteById(id) > 0;
    }

    @Override
    @EncodeOperation
    public Optional<Blog> insert(@EncodeParam Blog record) {
        int insert = context.getBlogMapper().insert(record);
        if (insert > 0) {
            return Optional.of(context.getBlogMapper().selectById(record.primaryKeyValue()));
        }
        return Optional.empty();
    }

    @Override
    public Optional<Blog> selectById(Integer id) {
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<Blog> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(Blog::getBlogId, id);
        return Optional.of(context.getBlogMapper().selectOne(lambdaQueryWrapper));
    }

    @Override
    public List<Blog> selectAll(String order) {
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<Blog> select = getColumnWrapper(wrapper.lambda());
            return context.getBlogMapper().selectList(select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<Blog> select = getColumnWrapper(wrapper.lambda());
        return context.getBlogMapper().selectList(select);
    }

    @Override
    public Page<Blog> selectAllPaged( Integer currentPage,
             Integer pageSize, String order) {
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        Page<Blog> page = new Page<>(currentPage, pageSize);
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<Blog> select = getColumnWrapper(wrapper.lambda());
            return context.getBlogMapper().selectPage(page, select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<Blog> select = getColumnWrapper(wrapper.lambda());
        return context.getBlogMapper().selectPage(page, select);
    }

    private LambdaQueryWrapper<Blog> getColumnWrapper(LambdaQueryWrapper<Blog> wrapper) {
        return wrapper.select(
                Blog::getBlogId
                    ,
                Blog::getBlogTextContent
                    ,
                Blog::getBlogPublisherId
                    ,
                Blog::getBlogPublishTime
                    ,
                Blog::getBlogAgreeNum
        );
    }

    @Override
    public Page<Blog> selectLikePaged( Integer currentPage,
             Integer pageSize, Blog record, String order) {
        Page<Blog> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            return context.getBlogMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        return context.getBlogMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
    }

    private LambdaQueryWrapper<Blog> getQueryWrapper(QueryWrapper<Blog>wrapper, Blog record) {
        LambdaQueryWrapper<Blog> lambda = wrapper.lambda();
        if (record.getBlogId() != null) {
            lambda = lambda.eq(Blog::getBlogId,record.getBlogId());
        }
        if (record.getBlogPublisherId() != null) {
            lambda = lambda.eq(Blog::getBlogPublisherId,record.getBlogPublisherId());
        }
        if (record.getBlogPublishTime() != null) {
            lambda = lambda.eq(Blog::getBlogPublishTime,record.getBlogPublishTime());
        }
        if (record.getBlogAgreeNum() != null) {
            lambda = lambda.eq(Blog::getBlogAgreeNum,record.getBlogAgreeNum());
        }
        if (record.getBlogTextContent() != null) {
            lambda = lambda.like(Blog::getBlogTextContent,record.getBlogTextContent ());
        }
        return lambda;
    }

    public int count(Blog record) {
        return context.getBlogMapper().selectCount(getQueryWrapper(new QueryWrapper<>(), record));
    }

    @Override
    @EncodeOperation
    public Optional<Blog> updateById(Integer id, @EncodeParam Blog record) {
        record.setBlogId(id);
        int update = context.getBlogMapper().updateById(record);
        if (update > 0) {
            QueryWrapper<Blog> wrapper = new QueryWrapper<>();
            LambdaQueryWrapper<Blog> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(Blog::getBlogId, id);
            return Optional.of(context.getBlogMapper().selectOne(getColumnWrapper(lambdaQueryWrapper)));
        }

        return Optional.empty();
    }
}
